package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 菜单控制层
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 通过用户名动态获取菜单数据并缓存至redis的3号库
     */
    @RequestMapping(value = "/getMenuByName")
    public Result getMenuByName(String username){
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.select(3);
            String menuList = jedis.get(username);
            if(menuList==null){
                List<Menu> mapList = menuService.getMenuByName(username);
                menuList = JSON.toJSONString(mapList);
                jedis.set(username,menuList);
            }
            List<Menu> mapList=JSON.parseObject(menuList,new TypeReference<List<Menu>>(){});
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }


    /**
     * 根据条件动态查询查询菜单列表
     */
    @RequestMapping("/getMenuList")
    public Result getMenuList(@RequestBody QueryPageBean queryPageBean){
        try {
            List<Menu> mapList = menuService.getMenuList(queryPageBean.getQueryString());
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 根据menuId删除菜单项
     */
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(Integer menuId){
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.select(3);
            jedis.flushDB();
            menuService.deleteById(menuId);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 回显一级菜单数据
     */
    @RequestMapping("/getLevel1Menu")
    public Result getLevel1Menu(){
        try {
            List<Menu>list=menuService.getLevel1Menu();
            return new Result(true,"回显一级菜单数据成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显一级菜单数据失败");
        }
    }

    /**
     * 新增菜单
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.select(3);
            jedis.flushDB();
            menuService.add(menu);
            return new Result(true,"新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增菜单失败");
        }

    }

    /**
     * 根据menuId查询Menu对象
     */
    @RequestMapping("/getMenu")
    public Result getMenu(Integer menuId ){
        try {
            Menu menu=menuService.getMenu(menuId);
            return new Result(true,"回显菜单数据成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显菜单数据失败");
        }
    }

    /**
     * 编辑菜单
     */
   @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
       try {
           Jedis jedis = jedisPool.getResource();
           jedis.select(3);
           jedis.flushDB();
           menuService.edit(menu);
           return new Result(true,"编辑菜单成功");
       }
       catch (RuntimeException ee){
           return new Result(false,ee.getMessage());
       }catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"编辑菜单失败");
       }
   }

    /**
     * 校验菜单名是否重复
     */
   @RequestMapping("/checkName")
    public boolean checkName(@RequestBody Map map ){
       Menu menu = menuService.findMenuByName((String) map.get("name"));
       if (map.get("id") != null){
           return (menu != null && map.get("id") != menu.getId());
       }
       return menu != null;
   }


}
